
//********Grid Creation***********
function initGrid(){
    $("#myGrid").flexigrid({
    url: '/grid/list',
    dataType: 'json',
    colModel : [
    {display: 'ID', name : 'id', width : 50, sortable : true, align: 'center'},
    {display: 'Email', name : 'email', width : 300, sortable : true, align: 'center'},
    {display: 'Name', name : 'login_name', width : 200, sortable : true, align: 'center'},
    {display: 'Status', name : 'status', width : 100, sortable : true, align: 'center'},
    {display: 'Role', name : 'role_name', width : 100, sortable : true, align: 'center'},
    {display: 'Actions', name : 'actions', width : 60, align: 'center'}
    ],

    buttons : [
    {name: 'Add New', bclass: 'add', onpress : add},
    {name: 'Delete', bclass: 'delete', onpress : test},
    {separator: true},
    {name: 'Select All', bclass: 'alert_selected', onpress : selectAll},
    {name: 'Deselect All', bclass: 'alert_selected', onpress : deselectAll},
    {name: 'Invert Selection', bclass: 'alert_selected',onpress: invertSelection}
    ],

    searchitems : [
    {display: 'Name', name : 'login_name'},
    {display: 'Email', name : 'email'}
    ],

    sortname: "u.login_name",
    sortorder: "asc",
    usepager: true,
    title: 'Users',
    useRp: true,
    rp: 10,
    showTableToggleBtn: true,
    width: 900,
    height: 250,
    singleSelect: false
    });
}
//********add new user function***********

function add(){
    window.location.href='/admin/adduser/';

}

//********Handel Delete Action***********

function test(com, grid){
    var selected_rows = $("#myGrid").find("tr.trSelected").get();
   if(selected_rows.length > 0){
        if(confirm('Are you sure?')){
                $(".trSelected > td:first-child > div:first-child").each( function(index, element){
                    var user_id=$(element).html();
                    if(com == 'Delete'){
                        $.post('/admin/deleteuser?id='+user_id, {}, function(data){});
                    }
                });
               setTimeout("$('#myGrid').flexReload()", 500)
                
         }        
   }else{ alert('you didnt select any row');}
}


//********Selection functions***********

function selectAll() {
    var rows = $("#myGrid").find("tr").get();
    if(rows.length > 0) {
    $.each(rows,function(i,n) {
        $(n).addClass("trSelected");
    });
    }
}

function deselectAll() {
    var selected_rows = $("#myGrid").find("tr").get();
    if(selected_rows.length > 0) {
    $.each(selected_rows,function(i,n) {
        $(n).removeClass("trSelected");
    });
    }
}

function invertSelection() {
    var rows = $("#myGrid").find("tr").get();
    $.each(rows,function(i,n) {
        $(n).toggleClass("trSelected");
    });
}